package com.jht.bletool2java.characteristic.ftms;


import android.bluetooth.BluetoothGattCharacteristic;
import android.util.Log;

import androidx.annotation.Keep;


import com.jht.bletool2java.characteristic.TranslateData;
import com.jht.bletool2java.util.ByteUtil;
import com.jht.bletool2java.util.ComputerUtil;

import top.codestudy.annotation_uuid.MyUUID;

@Keep
@MyUUID(uuid = "00002ad6-0000-1000-8000-00805f9b34fb")
public class SupportedResistanceRange implements TranslateData {


    private final static String TAG = "ResistanceRange";
    private static SupportedResistanceRange data;

    private byte[] MinimumResistanceLevel = new byte[2];
    private byte[] MaximumResistanceLevel = new byte[2];
    private byte[] MinimumIncrement = new byte[2];
    private byte[] valueData;
    private boolean isInvalidData = false;

    public static SupportedResistanceRange getInstance() {
        data = new SupportedResistanceRange();
        return data;
    }

    public SupportedResistanceRange() {
    }

    public SupportedResistanceRange(BluetoothGattCharacteristic characteristic) {
        byte[] value = characteristic.getValue();
        valueData = value;
        parseData(value);
        for (int i = 0; i < value.length; i++) {
            Log.i(TAG, "SupportedResistanceRange: i = " + i + "; ox" + Integer.toHexString(ByteUtil.byte1ToInt(value[i])));
        }
    }

    public void parseData(byte[] buffer) {
        System.arraycopy(buffer, 0, MinimumResistanceLevel, 0, 2);
        System.arraycopy(buffer, 2, MaximumResistanceLevel, 0, 2);
        System.arraycopy(buffer, 4, MinimumIncrement, 0, 2);
    }

    public double getMinimumResistanceLevel() {
        return ComputerUtil.multiply(ByteUtil.bytes2ToInt(MinimumResistanceLevel, 0), "0.1");
    }

    public double getMaximumResistanceLevel() {
        return ComputerUtil.multiply(ByteUtil.bytes2ToInt(MaximumResistanceLevel, 0), "0.1");
    }

    public double getMinimumIncrement() {
        return ComputerUtil.multiply(ByteUtil.bytes2ToInt(MinimumIncrement, 0), "0.1");
    }

    @Override
    public String convert2String() {
        return "SupportedResistanceRange:\nMinimumResistanceLevel : " + getMinimumResistanceLevel()
                + " \n MaximumResistanceLevel : " + getMaximumResistanceLevel()
                + " \n MinimumIncrement : " + getMinimumIncrement() + " " + "\n";
    }

    @Override
    public String merge(TranslateData translateData) {
        return null;
    }

    @Override
    public boolean hasMoreData() {
        return false;
    }

    @Override
    public byte[] getData() {

        return valueData;
    }

    @Override
    public boolean isInvalidData() {
        return isInvalidData;
    }

    @Override
    public void parseData(BluetoothGattCharacteristic characteristic, byte[] value) {
        isInvalidData = (value == null || value.length != 6);
        if (isInvalidData) {
            //非法数据
            init();
            valueData = value;
        } else {
            valueData = value;
            parseData(value);
            for (int i = 0; i < value.length; i++) {
                Log.i(TAG, "SupportedResistanceRange: i = " + i + "; ox" + Integer.toHexString(ByteUtil.byte1ToInt(value[i])));
            }
        }

    }

    private void init() {
        MinimumResistanceLevel = new byte[2];
        MaximumResistanceLevel = new byte[2];
        MinimumIncrement = new byte[2];
        valueData = null;
    }
}
